package tests.update;

import static org.junit.Assert.*;

import java.util.ArrayList;

import model.Budget;
import model.BudgetFactory;
import model.Note;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.workplicity.util.Helper;
import org.workplicity.worklet.WorkletContext;

import cc.test.bridge.NoteInterface;

/*
 * 
 * Attempts to change the name of an existing note
 * [currently this feature is not possible in the GUI]
 * 
 */

public class NoteUpdateText00
{
	Budget budget;
	BudgetFactory factory;
	Note note;

	@Before
	public void setUp() throws Exception
	{
		factory = new BudgetFactory();
		budget = (Budget) factory.create();
        budget.setName("Test budget");
        budget.setDescription("This is a test budget.");
        
        note = (Note) budget.createNote();
		note.setText("This is a note");
		budget.add(note);
		budget.commit();
	}

	@After
	public void tearDown() throws Exception
	{
		Helper.delete(budget, "Budgets", WorkletContext.getInstance());
	}

	@Test
	public void test()
	{
		try
		{
			budget.getNotes().get(0).setText("This is a test of our budget notes.");
			Boolean successful = budget.commit();
			
			if(successful)
			{
				ArrayList<NoteInterface> notes = budget.getNotes();
				
				for(NoteInterface n : notes)
				{
					if(n.getText().equals("This is a note"))
					{
						fail("Note text did not update");
					}
					else if(!n.getText().equals("This is a test of our budget notes."))
					{
						fail("Note test is not correct");
					}
				}
			}
			else
			{
				fail("Budget commit failed");
			}
		}
		catch(Exception e)
		{
			fail("Read content of note failed with exception: "+ e.getStackTrace());
		}
	}
}